package com.nakamachizu.controller;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.controller.validator.Validators;

import com.nakamachizu.model.MapData;
import com.nakamachizu.service.MapDataService;
import com.nakamachizu.util.ForwardUtil;

public class WelcomeController extends Controller {

    @Override
    public Navigation run() {
        
        String keyStr = requestScope("key");
        if(validate()){
            MapDataService mds = new MapDataService();
            MapData map = mds.getMapDataAccessKey(keyStr);
            requestScope("name", map.getAdminMailAddress());
            requestScope("expireDate", map.getExpire());
        }
        
        return forward(ForwardUtil.forwardJsp("welcome.jsp"));
    }
    
    protected boolean validate() {
        boolean result = false;
        Validators v = new Validators(request);
        v.add("key", v.required());

        result = v.validate();
        
        return result;
    }
    
    @Override
    protected Navigation handleError(Throwable error) {
        error.printStackTrace();
        return forward("error.jsp");
    }
}
